package com.cyj.dream.admin.controller.admin.system;

import com.alibaba.fastjson.JSONObject;
import com.cyj.dream.admin.model.request.SysRoleModuleRequest;
import com.cyj.dream.admin.service.system.SysModuleService;
import com.cyj.dream.admin.service.system.SysRoleModuleService;
import com.cyj.dream.core.aspect.annotation.ResponseResult;
import com.cyj.dream.core.aspect.annotation.log.LogRecord;
import com.cyj.dream.core.aspect.enums.OperatorType;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.IsEmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 角色模块关联关系控制器
 * @BelongsProject: fckj-restaurant
 * @BelongsPackage: com.fckj.fckjrestaurant.controller.admin.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-17 10:31
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@RestController
@ResponseResult
@Api(value = "roleModule", tags = "角色模块关联模块")
@RequestMapping(value = "/pc/roleModule", name = "角色模块关联模块")
public class SysRoleModuleController {

    @Autowired
    private SysRoleModuleService sysRoleModuleService;

    @Autowired
    private SysModuleService sysModuleService;

    /**
     * 为角色设置模块不带分页
     *
     * @param roleId 角色id
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @date 2021/6/10 10:55
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为角色设置模块不带分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "Long", required = true)
    })
    @LogRecord(title = "为角色设置模块不带分页", operatetype = OperatorType.SELECT)
    @RequestMapping(value = "/getModulesList", name = "为角色设置模块不带分页", method = RequestMethod.GET)
    public Map<String, Object> getModulesList(@RequestParam(value = "roleId", required = false) Long roleId) {
        log.info("进入不分页查询角色控制器");
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);

        if(IsEmptyUtils.isEmpty(roleId)){
            log.error("出现错误：参数缺失查询为角色设置模块不带分页失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        map.put("right", sysModuleService.getRoleModulesByRoleId(roleId));
        map.put("left", sysModuleService.getModulesLists());
        return map;
    }

    /**
     * 为角色设置模块
     *
     * @param sysRoleModuleRequest 角色模块关联关系请求类
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/5/17 10:18
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为角色设置模块", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRoleModuleRequest", value = "角色模块关联关系请求类", dataType = "SysRoleModuleRequest", required = true)
    })
    @LogRecord(title = "为角色设置模块", operatetype = OperatorType.INSERT)
    @RequestMapping(value = "/settingSysRoleModule", name = "为角色设置模块", method = RequestMethod.POST)
    public Map<String, Object> settingSysRoleModule(@RequestBody SysRoleModuleRequest sysRoleModuleRequest) {
        log.info("进入为角色设置模块方法控制器，参数为{}", JSONObject.toJSONString(sysRoleModuleRequest));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRoleModuleRequest.getSysRoleId()) || IsEmptyUtils.isEmpty(sysRoleModuleRequest.getSysModuleId())) {
            log.error("出现错误：参数缺失为角色设置模块失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Set<Long> sysModuleId = new HashSet<>();
        String[] array = sysRoleModuleRequest.getSysModuleId().split(",");
        for (String sysModuleIds : array) {
            sysModuleId.add(Long.parseLong(sysModuleIds));
        }

        map.put("info", sysRoleModuleService.settingSysRoleModule(sysRoleModuleRequest.getSysRoleId(), sysModuleId));
        return map;
    }

    /**
     * 为角色移除模块
     *
     * @param sysRoleModuleRequest 角色模块关联关系请求类
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @date 2021/5/17 10:18
     * @author ChenYongJia
     * @version 1.0
     */
    @ApiOperation(value = "为角色移除模块", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysRoleModuleRequest", value = "角色模块关联关系请求类", dataType = "SysRoleModuleRequest", required = true)
    })
    @LogRecord(title = "为角色移除模块", operatetype = OperatorType.DELETE)
    @RequestMapping(value = "/deleteSysRoleModule", name = "为角色移除模块", method = RequestMethod.DELETE)
    public Map<String, Object> deleteSysRoleModule(@RequestBody SysRoleModuleRequest sysRoleModuleRequest) {
        log.info("进入为角色移除模块方法控制器，参数为{}", JSONObject.toJSONString(sysRoleModuleRequest));
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        if (IsEmptyUtils.isEmpty(sysRoleModuleRequest.getSysRoleId()) || IsEmptyUtils.isEmpty(sysRoleModuleRequest.getSysModuleId())) {
            log.error("出现错误：参数缺失为角色移除模块失败！参数为空");
            throw new BizException(ResultCode.PARAM_NOT_COMPLETE);
        }

        Set<Long> sysModuleId = new HashSet<>();
        String[] array = sysRoleModuleRequest.getSysModuleId().split(",");
        for (String sysModuleIds : array) {
            sysModuleId.add(Long.parseLong(sysModuleIds));
        }

        map.put("info", sysRoleModuleService.deleteSysRoleModule(sysRoleModuleRequest.getSysRoleId(), sysModuleId));
        return map;
    }

}
